3259. Divisors

 

Find the smallest positive integer x that has exactly n divisors.

 

Input. One positive integer n (1 ≤ n ≤ 16).

 

Output. Print the smallest positive integer x that has exactly n divisors.

 

Sample input 1

Sample output 1

2

2

 

 

Sample input 2

Sample output 2

4

6

 

 

SOLUTION

number theory - divisors

 

Algorithm analysis

Iterate through the positive integers x = 1, 2, 3, … . For each x count the number of its divisors. As soon as a number with exactly n divisors is found, print it and terminate the program.

This solution runs within the time limits, since the value of n is small. For example, when n = 16, the answer is 120.

 

Algorithm implementation

The Divisors function counts the number of divisors of the number n by simply iterating over all possible divisors from 1 to n.

 

int Divisors(int n)

{

  int res = 0, i;

  for (i = 1; i <= n; i++)

    if (n % i == 0) res++;

  return res;

}

 

The main part of the program. Read the input value n, which represents the required number of divisors.

 

scanf("%d",&n);

 

Sequentially iterate through the positive integers i = 1, 2, 3, … and compute the number of divisors for each of them. As soon as a number i with exactly n divisors is found, the loop is terminated.

 

for (i = 1; ; i++)

{

  d = Divisors(i);

  if (d == n) break;

}

 

Print the answer.

 

printf("%d\n",i);